package com.lagou.controller;

import com.lagou.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/user")
@SessionAttributes("username") //向request中(model)中添加key为username时,会同步到session中
public class UserController {

    // 以get请求的方式请求该接口,必须携带accountName参数
    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"accountName"})
    public String quick(){
        System.out.println("项目启动了...");
        // 视图跳转 逻辑视图名
        return "success";
    }
    // 获取对象类型
    @RequestMapping("/pojoParam")
    public String quick1(User user){
        System.out.println(user.getId());
        System.out.println(user.getUsername());
        return "success";
    }
    // 获取对象类型
    @RequestMapping("/converterParam")
    public String quick2(Date birthday){
        System.out.println(birthday);
        return "success";
    }
    @RequestMapping("/findByPage")
    public String quick3(@RequestParam(name = "pageNo",defaultValue = "2",required = false)Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println(pageNum);
        System.out.println(pageSize);
        return "success";
    }

    @RequestMapping("/returnVoid")
    public void quick4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 直接向页面返回数据
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("欧阳钊你是最棒的！！！");
        // 请求转发,只是进行一次请求
        //request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
        // 重定向,进行两次请求 web-inf是一个安全目录,只是能进行服务器内部转发，不能外部访问
       // response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
    /**
     * modelAndView进行页面跳转:方式一
     */
    @RequestMapping("/returnModelAndView")
    public ModelAndView returnModelAndView(){
        ModelAndView view = new ModelAndView();
        System.out.println("------------------");
        // 设置模型数据
        view.addObject("username","方式一");
        // 设置视图名称,视图解析器拼接前缀和后缀
        view.setViewName("success");
        return  view;
    }
    /**
     * 方式二
     */
    @RequestMapping("/returnModelAndView2")
    public ModelAndView returnModelAndView2(ModelAndView view){
        System.out.println("------------------");
        // 设置模型数据
        view.addObject("username","方式一");
        // 设置视图名称,视图解析器拼接前缀和后缀
        view.setViewName("success");
        return  view;
    }
    /**
     * @SessionAttributes的效果:实现多个请求之间共享数据
     */
    @RequestMapping("/returnString")
    public String returnString(){
        return "success";
    }


    /**
     * ajax实现异步交互
     */
    @RequestMapping("/ajaxRequest")
    @ResponseBody
    public List<User> ajaxRequest(@RequestBody List<User> userList){
        System.out.println(userList);
        return userList;
    }



}
